package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO , HttpSession session){

        User user = (User) session.getAttribute("user");
        if (user == null) {//未登录状态
            return 2;
        }else {
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO,weibo);
            weibo.setUserId(user.getId());
            weibo.setCreated(new Date());
            weiboMapper.insert(weibo);
        return 1;
        }
    }


    @GetMapping("/selectIndex")
    public List<WeiboVO> selectIndexWeibo(){
        return weiboMapper.selectWeibo();
    }

    @GetMapping("/selectById")
    public WeiboDetailVO selectDetail(Integer id){
        return weiboMapper.selectWeiboById(id);
    }
}
